// pages/report_detail/report_detail.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    types: [],
    typeId: null,
    images: [],
    maxImage: 12,
  },
  check:function(e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      typeId: id
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.getJobInformTypes().then(types=>{this.setData({types})});
    this.setData({jobId: options.id});
  },
  uploadImage(event) {
    let page = this;
    let availableCount = page.data.maxImage - page.data.images.length;
    if(availableCount<1) {
      wx.showToast({title: '最多12张图片',icon:'none'});
      return;
    }
    wx.chooseImage({
      count: availableCount,
      success: res => {
        res.tempFiles.forEach(function(file){
          core.uploadFile({
            filePath: file.path,
            success(ret, response) {
              page.data.images.push(ret.data.url);
              page.setData({images: page.data.images,'course.images':page.data.images.join(',')});
            }
          });
        });
      }
    });
  },
  bindRemoveImage(event) {
    let index = event.currentTarget.index;
    this.data.images.splice(index, 1);
    this.setData({images: this.data.images});
  },
  bindSubmit(event) {
    let data =event.detail.value;
    let validator = new WeValidator({
      rules: {
        job_inform_type_id: {required:true},
        content: {required:true},
        contact: {maxlength: 40},
      },
      messages: {
        job_inform_type_id: {required:'请选择举报类型'},
        content: {required:'请填写举报原因'}
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    data.job_id = this.data.jobId;
    data.images = this.data.images.join(',');
    core.post({
      url: '/xilujob.job/inform', data: data,
      success(ret) {
        wx.navigateBack();
        wx.showToast({title: '谢谢您的举报，我们会认真核实',icon:'none'});
      }
    });
  }

});
